package com.supcon.exchangedata.service.hj;

import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoArea;
import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoAreaDetail;
import com.supcon.exchangedata.pojo.entity.oracle.CtGarbageRoute;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjCleanLine;
import com.supcon.exchangedata.repository.master.MasterBaseAreaRepository;
import com.supcon.exchangedata.repository.master.MasterBaseGeoAreaDetailRepository;
import com.supcon.exchangedata.repository.master.MasterGarbageRouteRepository;
import com.supcon.exchangedata.repository.master.MasterGisPointRepository;
import com.supcon.exchangedata.repository.slave6.SlaveHjBasicInfo3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/03/17/16:02
 * @Description:
 */
@Service
@Slf4j
public class GarbageRouteService {
    @Autowired
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Autowired
    private MasterGisPointRepository masterGisPointRepository;

    @Resource
    private MasterBaseGeoAreaDetailRepository masterBaseGeoAreaDetailRepository;

    @Resource
    private SlaveHjBasicInfo3 slaveHjBasicInfo3;

    @Resource
    private MasterGarbageRouteRepository masterGarbageRouteRepository;


    /**
     * 线路信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void transGarbageRoute() {
        List<HjCleanLine> all = slaveHjBasicInfo3.findAll();
        List<BaseGeoArea> baseGeoAreasByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(4017L);
        List<BaseGeoArea> removeList = new ArrayList<>();
        List<BaseGeoArea> updateList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(baseGeoAreasByAreaType)) {
            baseGeoAreasByAreaType.forEach((e) -> {
                if (!CollectionUtils.isEmpty(all)) {
                    Iterator<HjCleanLine> iterator = all.iterator();
                    boolean removeFlag = true;
                    while (iterator.hasNext()) {
                        HjCleanLine next = iterator.next();
                        //筛选测试数据
                        if (next.getCleanLineName().equals(e.getAreaName())) {
                            BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                            removeFlag = false;
                            e.setAreaCode("hjxlxxa" + next.getId());
                            e.setUpdateDate(LocalDateTime.now());
                            //线路编号
                            e.setExtension4(next.getCleanLineNo());
                            //车辆自编号
                            e.setExtension5(next.getCleanLineCar());

                            //开始运营时间
                            baseGeoAreaDetail.setSubsidiaryInformation1(next.getCleanLineStime());
                            //结束运营时间
                            baseGeoAreaDetail.setSubsidiaryInformation2(next.getCleanLineEtime());
                            //线路垃圾运输类型
                            baseGeoAreaDetail.setSubsidiaryInformation3(next.getCleanLineType());
                            e.setBaseGeoAreaDetail(baseGeoAreaDetail);

                            updateList.add(e);
                            iterator.remove();
                            break;
                        }
                    }
                    if (removeFlag) {
                        removeList.add(e);
                    }
                }


            });
        }

        deleteAreaAndPoint(removeList);
        updateGarbageRoute(updateList);
        if (!CollectionUtils.isEmpty(all)) {

            for (HjCleanLine station : all) {
                BaseGeoArea area = new BaseGeoArea();
                area.setAreaCode("hjxlxxa" + station.getId());

                area.setAreaName(station.getCleanLineName());
                area.setFullLevelName("环境→线路信息→" + station.getCleanLineName());
                area.setParentId(222834L);
                area.setAreaType(4017L);
                area.setUpdator("jpa-program");
                area.setUpdateDate(LocalDateTime.now());

                //线路编号
                area.setExtension4(station.getCleanLineNo());
                //车辆自编号
                area.setExtension5(station.getCleanLineCar());
                BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                //开始运营时间
                baseGeoAreaDetail.setSubsidiaryInformation1(station.getCleanLineStime());
                //结束运营时间
                baseGeoAreaDetail.setSubsidiaryInformation2(station.getCleanLineEtime());
                //线路及时率
                baseGeoAreaDetail.setSubsidiaryInformation3(station.getCleanLineType());
                //日处理能力
                BaseGeoArea save = masterBaseAreaRepository.save(area);
                baseGeoAreaDetail.setAreaId(save.getId());
                masterBaseGeoAreaDetailRepository.save(baseGeoAreaDetail);
            }

        }
    }

    /**
     * 更新垃圾线路相关图层信息
     *
     * @param updateList
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateGarbageRoute(List<BaseGeoArea> updateList) {
        if (!CollectionUtils.isEmpty(updateList)) {
            updateList.forEach((e) -> {
                e.setParentId(222834L);
                e.setAreaType(4017L);
                e.setUpdator("jpa-program");
                BaseGeoArea save = masterBaseAreaRepository.save(e);
                BaseGeoAreaDetail distinctByAreaId = masterBaseGeoAreaDetailRepository.findDistinctByAreaId(e.getId());
                if (distinctByAreaId == null) {
                    distinctByAreaId = new BaseGeoAreaDetail();
                }
                BaseGeoAreaDetail baseGeoAreaDetail = e.getBaseGeoAreaDetail();
                if (baseGeoAreaDetail != null) {
                    //容量
                    distinctByAreaId.setSubsidiaryInformation1(baseGeoAreaDetail.getSubsidiaryInformation1());

                    //开始运营时间
                    baseGeoAreaDetail.setSubsidiaryInformation1(baseGeoAreaDetail.getSubsidiaryInformation1());
                    //结束运营时间
                    baseGeoAreaDetail.setSubsidiaryInformation2(baseGeoAreaDetail.getSubsidiaryInformation2());
                    //线路及时率
                    baseGeoAreaDetail.setSubsidiaryInformation3(baseGeoAreaDetail.getSubsidiaryInformation3());
                }
                distinctByAreaId.setAreaId(save.getId());
                masterBaseGeoAreaDetailRepository.save(distinctByAreaId);

            });
        }
    }

    /**
     * 删除不需要的图层点位信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAreaAndPoint(List<BaseGeoArea> removeList) {
        if (!CollectionUtils.isEmpty(removeList)) {
            removeList.forEach((e) -> {
                masterBaseAreaRepository.deleteById(e.getId());
                masterGisPointRepository.deleteByObjIdAndYwCategory(e.getId(), String.valueOf(e.getAreaType()));
                masterBaseGeoAreaDetailRepository.deleteByAreaId(e.getId());
            });
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public void transGarbageRouteNew() {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String timePre = timeFormatter.format(LocalDate.now());
        List<HjCleanLine> all = slaveHjBasicInfo3.findAll();
        if (!CollectionUtils.isEmpty(all)) {
            List<CtGarbageRoute> garbageRoutes = new ArrayList<>();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            all.forEach(e -> {
                CtGarbageRoute route = new CtGarbageRoute();
                try {
                    LocalDateTime endTime = LocalDateTime.parse(timePre+" "+e.getCleanLineEtime(), dateTimeFormatter);
                    route.setOperationEndTime(endTime);
                    LocalDateTime startTime = LocalDateTime.parse(timePre+" "+e.getCleanLineStime(), dateTimeFormatter);
                    route.setOperationStartTime(startTime);
                } catch (Exception ex) {
                    log.info("{}", ex.getMessage());
                }
                //route.setLineLibraryId(e.getLineLibraryId());
                route.setLineNumber(e.getCleanLineNo());
                route.setCarSerialNumber(e.getCleanLineCar());
                //route.setRouteRate(e.getCleanLineType());
                route.setLineName(e.getCleanLineName());
                route.setRubbishType(e.getCleanLineType());
                garbageRoutes.add(route);
            });
            this.masterGarbageRouteRepository.deleteAll();
            garbageRoutes.forEach(e1 -> this.masterGarbageRouteRepository.save(e1));
        }
    }
}




